package com.opd.soeghtool;

import android.app.Activity;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.HashMap;


public class DebugActivity extends Activity {

    final String TAG = "SOEDBG";
    LinearLayout ll_debug_msg;
    String strTextView;
    GlobalVar mGlobalVar;
    TextView tv_debug_msg1;
    byte[] g_u8DataBuf;
    Button tv_debug_exit;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.debug_msg_layout);
        mGlobalVar = (GlobalVar)getApplication();
        ll_debug_msg = (LinearLayout)findViewById(R.id.ll_debug_msg);
        tv_debug_msg1 =  (TextView)findViewById(R.id.tv_debug_msg1);
        tv_debug_exit =  (Button) findViewById(R.id.tv_debug_exit);
        g_u8DataBuf = new byte[Transfer_Con.HID_PACKET_SIZE];
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        display.getRealMetrics(dm);

        tv_debug_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        strTextView = String.format("Android API: %d\n", Build.VERSION.SDK_INT);
        strTextView += String.format("Phone Resolution: %dX%d\n",dm.heightPixels,dm.widthPixels);
        //Usb
        if(mGlobalVar.myUsbManager!=null) {
            strTextView += "Usb Mng: " + mGlobalVar.myUsbManager.toString() + "\n";
            HashMap<String, UsbDevice> deviceList = mGlobalVar.myUsbManager.getDeviceList();
            if (!deviceList.isEmpty()) {
                StringBuffer sb = new StringBuffer();
                for (UsbDevice device : deviceList.values()) {
                    sb.append(device.toString());
                    sb.append("\n");
                    // 枚举到设备
                    //Log.d(TAG,String.format("vid=%x,Pid=%x",device.getVendorId(),device.getProductId()));
                    strTextView += "Usb: " + sb.toString() + "\n";
                }

        } else {
                strTextView += "Usb: deviceList empty!" + "\n";
            }

        }
        if(mGlobalVar.myUsbDevice!=null)
            strTextView+= "\nUsb Dev: "+mGlobalVar.myUsbDevice.toString()+"\n";
        if(mGlobalVar.myInterface!=null)
            strTextView+= "\nUsb Itf: "+mGlobalVar.myInterface.toString()+"\n";
        if(mGlobalVar.epOut!=null)
            strTextView+= "\nUsb epOut: "+mGlobalVar.epOut.toString()+"\n";
        if(mGlobalVar.epIn!=null)
            strTextView+= "\nUsb epInt: "+mGlobalVar.epIn.toString()+"\n";

        if(mGlobalVar.getbIsUsbConnect())
            strTextView+= "\nUsb Cnt: True \n";
        else
            strTextView+= "\nUsb Cnt: false \n";

        if(mGlobalVar.getbIsTransferMode())
            strTextView+= "\nUsb Mode: Transfer \n";
        else
            strTextView+= "\nUsb Mode: ISP \n";
        //Variable
        strTextView+= "Game support Total: "+String.valueOf(mGlobalVar.getGameGpTotalNum())+"\n";
        for(int a=0;a<mGlobalVar.getGameGpTotalNum();a++)
        {
            strTextView+= String.format("packName[%d]: %s\n",a,mGlobalVar.getPackageName(a));
        }
        strTextView+= "User Game Num: "+String.valueOf(mGlobalVar.getUserGameNum())+"\n";
        for(int a=0;a<mGlobalVar.getUserGameNum();a++)
        {
            strTextView+= String.format("UsrGmNm[%d]: %s\n",a,mGlobalVar.getUserGameName(a));
        }
        strTextView+= "List Game Num: "+String.valueOf(mGlobalVar.getGameListTotalNum())+"\n";
        strTextView+= "def exist Game Num: "+String.valueOf(mGlobalVar.getGameDefExist())+"\n";

        // game pad
        int temp=0;
        temp = mGlobalVar.get_Version(g_u8DataBuf);
        if(temp>0)
        {
            byte[]data = new byte[temp];
            System.arraycopy(g_u8DataBuf,0,data,0,temp);
            //mGlobalVar.setStrFwVer(new String(data));
            strTextView+= "FW Version : "+new String(data)+"\n";
        }
        else
        {
            strTextView+= "FW Version : Unknown\n";
        }

        temp = mGlobalVar.get_BuildDate(g_u8DataBuf);
        if(temp>0)
        {
            byte[]data = new byte[temp];
            System.arraycopy(g_u8DataBuf,0,data,0,temp);
            strTextView+= "FW Version : "+new String(data)+"\n";
        }
        else
            strTextView+= "FW BuildDate : Unknown\n";

        if(mGlobalVar.tranGet_Value((Transfer_Con.HID_CMD_GET_TEST_STAGE),0,0,g_u8DataBuf))
        {
            strTextView+= "gp Test : "+String.valueOf(g_u8DataBuf[0])+"\n";
        }
        else
            strTextView+= "gp Test : Unknown\n";

        if(mGlobalVar.tranGet_Value((Transfer_Con.HID_CMD_GET_SER_NUM),0,0,g_u8DataBuf))
        {
            int serailNum = 0,date = 0;
            for(int a=0;a<4;a++)
            {
                serailNum = serailNum + ((int)(g_u8DataBuf[a]&0xff)<<(a*8));
                date = date + ((int)(g_u8DataBuf[4+a]&0xff)<<(a*8));
                Log.d(TAG,"ser:"+String.valueOf(g_u8DataBuf[a])+" a="+String.valueOf(serailNum));
            }

            strTextView+= "gp Serial Num : "+String.valueOf(serailNum)+" at "+ String.valueOf(date)+"\n";
        }
        else
            strTextView+= "gp Serial Num : Unknown\n";


        if(mGlobalVar.get_CurPhone(g_u8DataBuf))
        {
            // = g_u8DataBuf[0];
            strTextView+= "gp Cur Phone : "+String.valueOf(g_u8DataBuf[0])+"\n";
        }
        else
        {
            strTextView+= "gp Cur Phone : Unknown\n";
        }

        if(mGlobalVar.tranGet_Value((Transfer_Con.HID_CMD_GET_GAME_MAX),0,0,g_u8DataBuf))
        {
            strTextView+= "gp game max : "+String.valueOf(g_u8DataBuf[0])+"\n";
        }
        else
            strTextView+= "gp game max : Unknown\n";

        if(mGlobalVar.get_CurGame(g_u8DataBuf))
        {
            strTextView+= "gp cur game : "+String.valueOf(g_u8DataBuf[0])+"\n";
        }
        else
            strTextView+= "gp cur game : Unknown\n";

        if(mGlobalVar.tranGet_Value((byte)(Transfer_Con.HID_CMD_GET_LEFT_HSEN),0,0,g_u8DataBuf))
        {
            strTextView+= "gp l rocker h : "+String.valueOf(g_u8DataBuf[0])+"\n";
        }
        else
            strTextView+= "gp l rocker h : Unknown\n";
        if(mGlobalVar.tranGet_Value((byte)(Transfer_Con.HID_CMD_GET_LEFT_VSEN),0,0,g_u8DataBuf))
        {
            strTextView+= "gp l rocker v : "+String.valueOf(g_u8DataBuf[0])+"\n";
        }
        else
            strTextView+= "gp l rocker v : Unknown\n";
        if(mGlobalVar.tranGet_Value((byte)(Transfer_Con.HID_CMD_GET_R_ROCK_MIN),0,0,g_u8DataBuf))
        {
            strTextView+= "gp r rocker h : "+String.valueOf(g_u8DataBuf[0])+"\n";
        }
        else
            strTextView+= "gp r rocker h : Unknown\n";
        if(mGlobalVar.tranGet_Value((byte)(Transfer_Con.HID_CMD_GET_R_ROCK_MAX),0,0,g_u8DataBuf))
        {
            strTextView+= "gp r rocker v : "+String.valueOf(g_u8DataBuf[0])+"\n";
        }
        else
            strTextView+= "gp r rocker v : Unknown\n";

        if(mGlobalVar.tranGet_Value((byte)(Transfer_Con.HID_CMD_GET_HW_SETTING),0,mGlobalVar.getIsSupportMenuKey()?0x80:0,g_u8DataBuf))
        {
            strTextView+= "gp wzry skill : "+String.valueOf(g_u8DataBuf[0])+"\n";
        }
        else
            strTextView+= "gp wzry skill : Unknown\n";

        if(mGlobalVar.tranGet_Value((byte)(Transfer_Con.HID_CMD_GET_PTTBL_TYPE),0,0,g_u8DataBuf))
        {
            strTextView+= "gp PtTbl from : "+((g_u8DataBuf[0]==0)?"default ":"User ")+"\n";
        }
        else
            strTextView+= "gp PtTbl from : Unknown\n";


        if(mGlobalVar.get_CurPtTbl((byte)(mGlobalVar.getDeviceMaxButton()*2),(byte)0,g_u8DataBuf))
        {
            short a;
            int []ptTbl = new int[mGlobalVar.getDeviceMaxButton()*2];
            String str="";
            for(a=0;a<mGlobalVar.getDeviceMaxButton();a++)
            {
                ptTbl[a] = (int)(g_u8DataBuf[2*a+1]&0xff)<<8;
                ptTbl[a] +=(int)(g_u8DataBuf[2*a]&0xff);
                if(a%2==0)
                    str+="["+Transfer_Con.tButtonName[a/2]+"]: ";
                str+=String.valueOf(ptTbl[a])+", ";
            }
            //Log.d(TAG,str);
            if(mGlobalVar.get_CurPtTbl((byte)(mGlobalVar.getDeviceMaxButton()*2),(byte)1,g_u8DataBuf))
            {
                //str="";
                for(a=0;a<mGlobalVar.getDeviceMaxButton();a++)
                {
                    ptTbl[mGlobalVar.getDeviceMaxButton()+a] = (int)(g_u8DataBuf[2*a+1]&0xff)<<8;
                    ptTbl[mGlobalVar.getDeviceMaxButton()+a] += (int)(g_u8DataBuf[2*a]&0xff);
                    if(a%2==0)
                        str+="["+Transfer_Con.tButtonName[(mGlobalVar.getDeviceMaxButton()+a)/2]+"]: ";
                    str+=String.valueOf(ptTbl[a])+", ";
                }
                //Log.d(TAG,str);
            }
            strTextView+= "gp PtTbl : "+str+"\n";
        }
        else
            strTextView+= "gp Load PtTbl fail !\n";

        tv_debug_msg1.setText(strTextView);
    }
}
